//
//  SLHomeViewController.m
//  SoccerLottery
//
//  Created by chuanshuangzhang chuan shuang on 15/12/24.
//  Copyright © 2015年 chuanshuangzhang. All rights reserved.
//

#import "SLHomeViewController.h"
#import "SLChargeViewController.h"
#import "SLFeedTableViewCell.h"
#import "SLFeedModel.h"
#import "SLLoginViewController.h"
#import "SLRegisterViewController.h"

@interface SLHomeViewController ()<UITableViewDataSource,UITableViewDelegate,SLHomeAccountViewDelegate>

@property (nonatomic,strong) NSDate *lastUpdateDate;

@end

@implementation SLHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imagePreview = [[SLImagePreview alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, App_Frame_Width * 0.475)];
    [self.view addSubview:_imagePreview];

    _bannerView = [[UIView alloc]initWithFrame:CGRectMake(0, _imagePreview.bottom, App_Frame_Width, App_Frame_Width*0.1875)];
    _bannerView.backgroundColor = RGBACOLOR(230, 231, 232, 1.0);
    [self.view addSubview:_bannerView];

    _tableView = [[UITableView alloc]init];
//    [_tableView registerClass:[SLFeedTableViewCell class] forCellReuseIdentifier:@"SLFeedTableViewCell"];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.frame = CGRectMake(0, _bannerView.bottom, App_Frame_Width,self.view.height-_bannerView.bottom-114);
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    
    _accountView = [[SLHomeAccountView alloc]initWithFrame:_bannerView.bounds];
    _accountView.delegate = self;
    [_bannerView addSubview:_accountView];
    
    
    _placeholderLabel = [[UILabel alloc]init];
    _placeholderLabel.textAlignment = NSTextAlignmentCenter;
    _placeholderLabel.font = [UIFont fontWithName:@"Arial" size:16];
    _placeholderLabel.bounds = CGRectMake(0, 0, App_Frame_Width, 30);
    _placeholderLabel.center = CGPointMake(App_Frame_Width/2.0, _bannerView.bottom+_tableView.height/2.0);
    
    _placeholderLabel.alpha  =0.0;
    
    [self.view addSubview:_placeholderLabel];
    if(![VWebService isConnected]){
        _placeholderLabel.alpha  = 1.0;
       self.placeholderLabel.text = @"无网络链接";
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if([UserModel sharedInstance].phone.length < 11)
    {
        self.accountView.accountLabel.alpha = 0.0;
        self.accountView.loginBtn.alpha = 1.0;
    }else {
        self.accountView.accountLabel.alpha = 1.0;
        self.accountView.loginBtn.alpha = 0.0;
        self.accountView.accountLabel.text = [UserModel sharedInstance].phone;
    }
    _accountView.amountLabel.text = [NSString stringWithFormat:@"金币：%@",[UserModel sharedInstance].coin];
    if([SLCache sharedInstance].feedArray == nil){
        [SLNewNetMgr getPicture:^(id result, BOOL status, NSError *error) {
            if(NET_SUCCESS(result, status)){
                [SLCache sharedInstance].feedArray = result[@"result"];
                _imagePreview.imageArray = [NSArray arrayWithArray:[SLCache sharedInstance].feedArray];
            }
        }];
    }else if(![SLCache sharedInstance].feedLoaded){
        _imagePreview.imageArray = [NSArray arrayWithArray:[SLCache sharedInstance].feedArray];
    }
    if(_lastUpdateDate == nil){
        _lastUpdateDate = [NSDate date];
    }else {
        NSDate *date = [NSDate date];
        NSInteger t = [Utils timeIntervalFromDate:_lastUpdateDate toDate:date];
        if(t < 1){
            _shouldLoadFeed = NO;
            return;
        }
        _lastUpdateDate = date;
    }
    _shouldLoadFeed = YES;
}

- (void)loadRecommend:(NSString *)type
{
   [SLNewNetMgr getRecommend:[UserModel sharedInstance].userId type:type pageCount:[NSString stringWithFormat:@"%d",100] pageIndex:[NSString stringWithFormat:@"%d",1] block:^(id result, BOOL status, NSError *error) {
       if(NET_SUCCESS(result, status)){
           NSArray *array = result[@"result"];
           [self.dataSorce removeAllObjects];
           for (NSDictionary *dic in array) {
               SLFeedModel *model = [[SLFeedModel alloc]init];
               model.title = dic[@"Title"];
               model.time = dic[@"GameTime"];
               model.price = CS(dic[@"Points"]);
               model.detail = dic[@"Details"];
               model.type = CS(dic[@"Type"]);
               model.feedId = CS(dic[@"ID"]);
               model.result = CS(dic[@"Result"]);
               model.status = CS(dic[@"Status"]);
               model.score = CS(dic[@"Score"]);
               if([model.status integerValue]==0){
                 model.status = [SLDBManager feedStatus:model.feedId];
               }else{
                   [SLDBManager saveFeed:model.feedId status:@"1"];
               }
               [self.dataSorce addObject:model];
           }
           
           [self.tableView reloadData];
           if(self.dataSorce.count <=0){
               self.placeholderLabel.alpha = 1.0;
               self.placeholderLabel.text = @"暂无推介信息";
           }else {
               self.placeholderLabel.alpha = 0.0;
           }
       }
   }];
}
- (NSMutableArray *)dataSorce {
    if(_dataSorce == nil){
        _dataSorce = [NSMutableArray array];
    }
    return _dataSorce;
}
#pragma Mark TableView delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSorce.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = RGBACOLOR(216, 216,216, 1.0);
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}
#pragma Mark AccountView delegate

- (void)accountViewDidSelectCharge:(SLHomeAccountView *)accountView
{
    if([UserModel sharedInstance].phone.length <11){
        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"建议登录后再充值，金币可以同步到您的账号" delegate:self cancelButtonTitle:@"暂时不" otherButtonTitles:@"去登录", nil];
        alterView.tag = 1000;
        [alterView show];
        return;
    }
    SLChargeViewController *vc = [[SLChargeViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)accountViewDidSelectLogin:(SLHomeAccountView *)accountView
{
    SLLoginViewController *vc = [[SLLoginViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1){
        SLLoginViewController *vc = [[SLLoginViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        SLChargeViewController *vc = [[SLChargeViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
